package org.ilc.task;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author malongbo
 */
public class TaskQueue {
    private Queue queue;
    private TaskExecutor executor;

    /**
     *
     * @param minThreads
     * @param maxThreads
     */
    public TaskQueue (int minThreads,int maxThreads) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(minThreads, maxThreads, 0L,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable> ());
        this.queue = new Queue ();
        executor = new TaskExecutor (threadPoolExecutor, queue);
    }

    /**
     *
     * @return
     */
    public Queue getQueue() {
        return queue;
    }

    /**
     *
     * @return
     */
    public TaskExecutor getExecutor() {
        return executor;
    }

    public void start () {
        if (executor != null) {
            executor.startWork ();
            return;
        }

        throw new NullPointerException ("Task executor is null");
    }

    public void stop () {
        if (executor != null) {
            executor.stopWork ();
        }
    }
}
